home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 276_01 / a16eval.c < prev    next >
C/C++ Source or Header  |  1989-10-01  |  10KB  |  420 lines

  1. /*
  2.     HEADER:        CUG276;
  3.     TITLE:        PIC1650 Cross-Assembler (Portable);
  4.     FILENAME:    A16EVAL.C;
  5.     VERSION:    0.0;
  6.     DATE:        06/15/1989;
  7.     SEE-ALSO:    A16.H;
  8.     AUTHORS:    William C. Colley III;
  9. */
  10.  
  11. /*
  12.              PIC1650 Cross-Assembler in Portable C
  13.  
  14.          Copyright (c) 1985,1987,1989 William C. Colley, III
  15.  
  16. Revision History:
  17.  
  18. Ver    Date        Description
  19.  
  20. 0.0    JUNE 1989    Derived from version 0.2 of my portable 8085 cross-
  21.             assembler.  WCC3.
  22.  
  23. This file contains the assembler's expression evaluator and lexical analyzer.
  24. The lexical analyzer chops the input character stream up into discrete tokens
  25. that are processed by the expression analyzer and the line assembler.  The
  26. expression analyzer processes the token stream into unsigned results of
  27. arithmetic expressions.
  28. */
  29.  
  30. /*  Get global goodies:  */
  31.  
  32. #include "a16.h"
  33.  
  34. /*  Get access to global mailboxes defined in A16.C:            */
  35.  
  36. extern char line[];
  37. extern int filesp, forwd, pass;
  38. extern unsigned pc;
  39. extern FILE *filestk[], *source;
  40. extern TOKEN token;
  41.  
  42. /*  Expression analysis routine.  The token stream from the lexical    */
  43. /*  analyzer is processed as an arithmetic expression and reduced to an    */
  44. /*  unsigned value.  If an error occurs during the evaluation, the    */
  45. /*  global flag    forwd is set to indicate to the line assembler that it    */
  46. /*  should not base certain decisions on the result of the evaluation.    */
  47.  
  48. static int bad;
  49.  
  50. unsigned expr()
  51. {
  52.     SCRATCH unsigned u;
  53.     unsigned eval();
  54.  
  55.     bad = FALSE;
  56.     u = eval(START);
  57.     return bad ? 0 : u;
  58. }
  59.  
  60. static unsigned eval(pre)
  61. unsigned pre;
  62. {
  63.     register unsigned op, u, v;
  64.     TOKEN *lex();
  65.     void exp_error(), unlex();
  66.  
  67.     for (;;) {
  68.     u = op = lex() -> valu;
  69.     switch (token.attr & TYPE) {
  70.         case SEP:
  71.         case EOL:    unlex();  exp_error('E');  return;
  72.  
  73.         case OPR:    if (!(token.attr & UNARY)) { exp_error('E');  break; }
  74.             u = eval((op == '+' || op == '-') ?
  75.                 (unsigned) UOP1 : token.attr & PREC);
  76.             switch (op) {
  77.                 case '-':    u = word(-u);  break;
  78.  
  79.                 case NOT:    u ^= 0xffff;  break;
  80.  
  81.                 case HIGH:    u = high(u);  break;
  82.  
  83.                 case LOW:    u = low(u);  break;
  84.             }
  85.  
  86.         case VAL:    
  87.         case STR:    for (;;) {
  88.                 op = lex() -> valu;
  89.                 switch (token.attr & TYPE) {
  90.                 case SEP:
  91.                 case EOL:   if (pre == LPREN) exp_error('(');
  92.                         unlex();  return u;
  93.  
  94.                 case STR:
  95.                 case VAL:   exp_error('E');  break;
  96.  
  97.                 case OPR:   if (!(token.attr & BINARY)) {
  98.                         exp_error('E');  break;
  99.                         }
  100.                         if ((token.attr & PREC) >= pre) {
  101.                         unlex();  return u;
  102.                         }
  103.                         if (op != ')')
  104.                         v = eval(token.attr & PREC);
  105.                         switch (op) {
  106.                         case '+':   u += v;  break;
  107.  
  108.                         case '-':   u -= v;  break;
  109.  
  110.                         case '*':   u *= v;  break;
  111.  
  112.                         case '/':   u /= v;  break;
  113.  
  114.                         case MOD:   u %= v;  break;
  115.  
  116.                         case AND:   u &= v;  break;
  117.  
  118.                         case OR:    u |= v;  break;
  119.  
  120.                         case XOR:   u ^= v;  break;
  121.  
  122.                         case '<':   u = u < v;  break;
  123.  
  124.                         case LE:    u = u <= v;  break;
  125.  
  126.                         case '=':   u = u == v;  break;
  127.  
  128.                         case GE:    u = u >= v;  break;
  129.  
  130.                         case '>':   u = u > v;  break;
  131.  
  132.                         case NE:    u = u != v;  break;
  133.  
  134.                         case SHL:   if (v > 15)
  135.                                 exp_error('E');
  136.                                 else u <<= v;
  137.                                 break;
  138.  
  139.                         case SHR:   if (v > 15)
  140.                                 exp_error('E');
  141.                                 else u >>= v;
  142.                                 break;
  143.  
  144.                         case ')':   if (pre == LPREN)
  145.                                 return u;
  146.                                 exp_error('(');
  147.                                 break;
  148.                         }
  149.                         clamp(u);
  150.                         break;
  151.                 }
  152.             }
  153.             break;
  154.     }
  155.     }
  156. }
  157.  
  158. static void exp_error(c)
  159. char c;
  160. {
  161.     forwd = bad = TRUE;  error(c);
  162. }
  163.  
  164. /*  Lexical analyzer.  The source input character stream is chopped up    */
  165. /*  into its component parts and the pieces are evaluated.  Symbols are    */
  166. /*  looked up, operators are looked up, etc.  Everything gets reduced    */
  167. /*  to an attribute word, a numeric value, and (possibly) a string    */
  168. /*  value.                                */
  169.  
  170. static int eol;
  171. static int oldt = FALSE;
  172. static char *lptr;
  173.  
  174. TOKEN *lex()
  175. {
  176.     SCRATCH char c, d, *p;
  177.     SCRATCH unsigned b;
  178.     SCRATCH OPCODE *o;
  179.     SCRATCH SYMBOL *s;
  180.     OPCODE *find_operator();
  181.     SYMBOL *find_symbol();
  182.     void exp_error(), make_number(), pops(), pushc(), trash();
  183.  
  184.     if (oldt) { oldt = FALSE;  return &token; }
  185.     trash();
  186.     if (isalph(c = popc())) {
  187.     pushc(c);  pops(token.sval);
  188.     if (!strcmp(token.sval,"$")) {
  189.         token.attr = VAL;
  190.         token.valu = pc;
  191.     }
  192.     else if (o = find_operator(token.sval)) {
  193.         token.attr = o -> attr;
  194.         token.valu = o -> valu;
  195.     }
  196.     else {
  197.         token.attr = VAL;  token.valu = 0;
  198.         if (s = find_symbol(token.sval)) {
  199.         token.valu = s -> valu;
  200.         if (pass == 2 && s -> attr & FORWD) forwd = TRUE;
  201.         }
  202.         else exp_error('U');
  203.     }
  204.     }
  205.     else if (isnum(c)) {
  206.     pushc(c);  pops(token.sval);
  207.     for (p = token.sval; *p; ++p);
  208.     switch (toupper(*--p)) {
  209.         case 'B':    b = 2;  break;
  210.  
  211.         case 'O':
  212.         case 'Q':    b = 8;  break;
  213.  
  214.         default:    ++p;
  215.         case 'D':    b = 10;  break;
  216.  
  217.         case 'H':    b = 16;  break;
  218.     }
  219.     *p = '\0';  make_number(b);
  220.     }
  221.     else switch (c) {
  222.     case '(':   token.attr = UNARY + LPREN + OPR;
  223.             goto opr1;
  224.  
  225.     case ')':   token.attr = BINARY + RPREN + OPR;
  226.             goto opr1;
  227.  
  228.     case '+':   token.attr = BINARY + UNARY + ADDIT + OPR;
  229.             goto opr1;
  230.  
  231.     case '-':   token.attr = BINARY + UNARY + ADDIT + OPR;
  232.             goto opr1;
  233.  
  234.     case '*':   token.attr = BINARY + UNARY + MULT + OPR;
  235.             goto opr1;
  236.  
  237.     case '/':   token.attr = BINARY + MULT + OPR;
  238. opr1:            token.valu = c;
  239.             break;
  240.  
  241.     case '<':   token.valu = c;
  242.             if ((c = popc()) == '=') token.valu = LE;
  243.             else if (c == '>') token.valu = NE;
  244.             else pushc(c);
  245.             goto opr2;
  246.  
  247.     case '=':   token.valu = c;
  248.             if ((c = popc()) == '<') token.valu = LE;
  249.             else if (c == '>') token.valu = GE;
  250.             else pushc(c);
  251.             goto opr2;
  252.  
  253.     case '>':   token.valu = c;
  254.             if ((c = popc()) == '<') token.valu = NE;
  255.             else if (c == '=') token.valu = GE;
  256.             else pushc(c);
  257. opr2:            token.attr = BINARY + RELAT + OPR;
  258.             break;
  259.  
  260.     case '\'':
  261.     case '"':   token.attr = STR;
  262.             for (p = token.sval; ; ++p) {
  263.             if ((d = getc(source)) == EOF) d = '\n';
  264.             if ((d &= 0177) == '\n') {
  265.                 ungetc('\n',source);  exp_error('"');  break;
  266.             }
  267.             if ((*p = *lptr++ = d) == c) {
  268.                 if ((d = getc(source)) == EOF) d = '\n';
  269.                 if ((d &= 0177) == c) *lptr++ = d;
  270.                 else { ungetc(d,source);  break; }
  271.             }
  272.             }
  273.             *p = '\0';
  274.             if ((token.valu = token.sval[0]) && token.sval[1])
  275.             token.valu = (token.valu << 8) + token.sval[1];
  276.             break;
  277.  
  278.     case ',':   token.attr = SEP;
  279.             break;
  280.  
  281.         case '\n':  token.attr = EOL;
  282.             break;
  283.     }
  284.     return &token;
  285. }
  286.  
  287. static void make_number(base)
  288. unsigned base;
  289. {
  290.     SCRATCH char *p;
  291.     SCRATCH unsigned d;
  292.     void exp_error();
  293.  
  294.     token.attr = VAL;
  295.     token.valu = 0;
  296.     for (p = token.sval; *p; ++p) {
  297.     d = toupper(*p) - (isnum(*p) ? '0' : 'A' - 10);
  298.     token.valu = token.valu * base + d;
  299.     if (!ishex(*p) || d >= base) { exp_error('D');  break; }
  300.     }
  301.     clamp(token.valu);
  302.     return;
  303. }
  304.  
  305. int isalph(c)
  306. char c;
  307. {
  308.     return (c >= '@' && c <= '~') || (c >= '#' && c <= '&') ||
  309.     c == '!' || c == '.' || c == ':' || c == '?';
  310. }
  311.  
  312. static int isnum(c)
  313. char c;
  314. {
  315.     return c >= '0' && c <= '9';
  316. }
  317.  
  318. static int ishex(c)
  319. char c;
  320. {
  321.     return isnum(c) || ((c = toupper(c)) >= 'A' && c <= 'F');
  322. }
  323.  
  324. static int isalnum(c)
  325. char c;
  326. {
  327.     return isalph(c) || isnum(c);
  328. }
  329.  
  330. /*  Push back the current token into the input stream.  One level of    */
  331. /*  pushback is supported.                        */
  332.  
  333. void unlex()
  334. {
  335.     oldt = TRUE;
  336.     return;
  337. }
  338.  
  339. /*  Get an alphanumeric string into the string value part of the    */
  340. /*  current token.  Leading blank space is trashed.            */
  341.  
  342. void pops(s)
  343. char *s;
  344. {
  345.     void pushc(), trash();
  346.  
  347.     trash();
  348.     for (; isalnum(*s = popc()); ++s);
  349.     pushc(*s);  *s = '\0';
  350.     return;
  351. }
  352.  
  353. /*  Trash blank space and push back the character following